package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.dal.service.mark.InsMarkBloggerBgsDayDao;
import com.gugee.ins.common.dal.service.mark.InsMarkBloggerBgsMonthDao;
import com.gugee.ins.common.dal.service.mark.InsMarkBloggerBgsWeekDao;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerBgsDao;
import com.gugee.ins.common.dto.RankTaskParam;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * ins 带货播主打点
 * @author huangxin
 */
@Component
@Slf4j
public class InsBloggerBgsMarkSvc extends AbstractMarkRankProducerHandler {

    /**
     *
     */
    private static final String DATE_FORMATTER = "yyyyMMdd";

    /**
     *
     */
    @Autowired
    InsStBloggerBgsDao insStBloggerBgsDao;

    /**
     *
     */
    @Autowired
    InsMarkBloggerBgsDayDao insMarkBloggerBgsDayDao;

    /**
     *
     */
    @Autowired
    InsMarkBloggerBgsWeekDao insMarkBloggerBgsWeekDao;

    /**
     *
     */
    @Autowired
    InsMarkBloggerBgsMonthDao insMarkBloggerBgsMonthDao;

    /**
     * 执行
     */
    @Override
    public void handleRank(RankTaskParam taskParam) {
        long maxId;
        long startId = 1;
        long pageSize = taskConfig.getInsBloggerBgsMarkDayPageSize() > 0 ? taskConfig.getInsBloggerBgsMarkDayPageSize() : 1000;
        long endId = pageSize;
        if(taskParam.getId() != null && taskParam.getId() > 0){
            startId = taskParam.getId();
            endId = taskParam.getId();
            maxId = taskParam.getId() + 1L;
        } else {
            maxId = objectTotals();
        }

        long dateCode = 0;
        if(!StringUtils.isEmpty(taskParam.getDateCode())){
            dateCode = getDateTime(taskParam.getDateCode());
        }

        // 循环爬取的id打点
        while(endId < maxId || startId == 1){
            try{
                mark(startId,endId,taskParam.getTypeCode());
                markDay(startId,endId,taskParam.getTypeCode(),dateCode);
                markWeek(startId,endId,taskParam.getTypeCode(),dateCode);
                markMonth(startId,endId,taskParam.getTypeCode(),dateCode);
            }catch (Exception e){
                log.info("ins 带货达人打点失败 id, msg :{}", e.getMessage());
                XxlJobLogger.log("ins 带货达人打点失败 id, msg :{}", e.getMessage());
            }
            startId += pageSize;
            endId += pageSize;
        }
    }

    /**
     * 获取最大的id
     * @return
     */
    public long objectTotals() {
        return insBloggerMainDao.getMaxId();
    }



    /**
     * 近30天打点
     * @param typeCode
     */
    public void mark(long startId,long endId,int typeCode){
        if(typeCode == 0 || typeCode == 1){
            try{
                insStBloggerBgsDao.insertEmptyData(startId,endId);
                long limitTimeEnd = System.currentTimeMillis() / 1000;
                long limitTimeStart = limitTimeEnd - 3600 * 24 * 30;
                insStBloggerBgsDao.updateSt(startId,endId,limitTimeStart,limitTimeEnd);
            }catch (Exception e){
                log.error("ins 带货达人30天统计打点失败 startId:{},endId:{}, msg :{}",startId,endId, e.getMessage());
            }
        }
    }

    /**
     * 日打点
     * @param typeCode
     */
    public void markDay(long startId,long endId,int typeCode,long dateCode){
        if(typeCode == 0 || typeCode == 2){
            try{
                Calendar calendar = Calendar.getInstance();
                // 设置时间
                if(dateCode > 0){
                    calendar.setTimeInMillis(dateCode);
                }
                calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
                calendar.set(Calendar.HOUR_OF_DAY,0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                long limitTimeEnd = calendar.getTimeInMillis() / 1000;
                long limitTimeStart = limitTimeEnd - 24 * 3600;

                int markDay = Integer.parseInt(DateTimeUtils.formatSecond(limitTimeStart,DATE_FORMATTER,"UTC"));
                insMarkBloggerBgsDayDao.insertMark(startId,endId,limitTimeStart,limitTimeEnd - 1,markDay);
            }catch (Exception e){
                log.error("ins 带货达人日统计打点失败 startId:{},endId:{}, msg :{}",startId,endId, e.getMessage());
            }
        }
    }

    /**
     * 周打点
     * @param typeCode
     */
    public void markWeek(long startId,long endId,int typeCode,long dateCode){
        Calendar calendar = Calendar.getInstance();
        // 设置时间
        if(dateCode > 0){
            calendar.setTimeInMillis(dateCode);
        }
        if(typeCode == 3 || (typeCode == 0 && isMonday(calendar))){
            try{
                calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
                calendar.set(Calendar.HOUR_OF_DAY,0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
                long limitTimeEnd = calendar.getTimeInMillis() / 1000 + 24 * 3600 - 1;
                long limitTimeStart = limitTimeEnd - 7 * 24 * 3600 + 1;

                int markDay = Integer.parseInt(DateTimeUtils.formatSecond(limitTimeEnd,DATE_FORMATTER,"UTC"));
                insMarkBloggerBgsWeekDao.insertMark(startId,endId,limitTimeStart,limitTimeEnd,markDay);
            }catch (Exception e){
                log.error("ins 带货达人周统计打点失败 startId:{},endId:{}, msg :{}",startId,endId, e.getMessage());
            }
        }
    }

    /**
     * 月打点
     * @param typeCode
     */
    public void markMonth(long startId,long endId,int typeCode,long dateCode){
        Calendar calendar = Calendar.getInstance();
        // 设置时间
        if(dateCode > 0){
            calendar.setTimeInMillis(dateCode);
        }
        if(typeCode == 4 || (typeCode == 0 && isFirstDayOfMonth(calendar))){
            try{
                calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
                calendar.set(Calendar.HOUR_OF_DAY,0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
                long limitTimeStart = calendar.getTimeInMillis() / 1000;
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                long limitTimeEnd = calendar.getTimeInMillis() / 1000 + 24 * 3600 - 1;

                int markDay = Integer.parseInt(DateTimeUtils.formatSecond(limitTimeEnd,DATE_FORMATTER,"UTC"));
                insMarkBloggerBgsMonthDao.insertMark(startId,endId,limitTimeStart,limitTimeEnd,markDay);
            }catch (Exception e){
                log.error("ins 带货达人月统计打点失败 startId:{},endId:{}, msg :{}",startId,endId, e.getMessage());
            }
        }
    }


}
